package edu.mtu.citizenscience.streamapplication.activity.main;

import edu.mtu.citizenscience.streamapplication.activity.DBHelper;
import edu.mtu.citizenscience.streamapplication.activity.R;
import edu.mtu.citizenscience.streamapplication.activity.R.layout;
import edu.mtu.citizenscience.streamapplication.activity.R.menu;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Bryan Becker <bcbecker@mtu.edu>
 * @copyright (c) 2013 MichiganTech <http://www.mtu.edu>
 * 
 * Report Guide Activity that contains a list of buttons which users
 * can press to input more detailed information regarding their report.
 * This is the central activity that controls navigation for editing and 
 * creation of a report. The buttons will display if certain sections have
 * been completed or not within the report. It also contains the cancel
 * or completion button for the entire report. 
 */
public class ReportGuide extends Activity
{
    // reference var for passing the boolean editing between activities
	public static final String EDITING = "EDITING";
	
	//row id of the report we are creating/editing
	private Long rowID;
	//database helper
	private DBHelper myDbHelper;
	
	//image views for the image to be displayed on each button
	private ImageView locationV;
	private ImageView culvertV;
	private ImageView streamV;
	private ImageView roadV;
	private ImageView erosionV;
	private ImageView treatmentV;
	private ImageView photoNotesV;
	
	//delete button
	private Button delete;
	//save button
	private Button save;
	//custom header bar
	private TextView header;
	
	//are we editing a report or creating one
	boolean editing;
	
	/**
	 * Report Guide Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		//set the content view and enable the custom title
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_report_guide);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		
		myDbHelper = new DBHelper(this);
		//open our database 
		myDbHelper.open();
		
		//retrieve our row id from the previous activity(default to -1 otherwise)
		rowID = this.getIntent().getLongExtra(SCLocationActivity.ROW_ID, -1);
		
		//initialize up our Image View variables
		locationV = (ImageView) findViewById(R.id.location_complete);
		culvertV = (ImageView) findViewById(R.id.culvert_complete);
		streamV = (ImageView) findViewById(R.id.stream_complete);
		roadV = (ImageView) findViewById(R.id.road_complete);
		erosionV = (ImageView) findViewById(R.id.erosion_complete);
		treatmentV = (ImageView) findViewById(R.id.treatment_complete);
		photoNotesV = (ImageView) findViewById(R.id.photo_notes_complete);
		
		//initialize up our save button
		save = (Button) findViewById(R.id.completeButton);
		//initialize our delete button
		delete = (Button) findViewById(R.id.deleteButton);
		
		//retrieve the editing boolean from the previous activity (default to false otherwise)
        editing = this.getIntent().getBooleanExtra(this.EDITING, false);
        
		header = (TextView) findViewById(R.id.header_text);
		//if we are editing an already completed report
		if(editing){
			//set the custom header title to "View/Edit Reports"
			header.setText("View/Edit Reports");
			//change the save button text to "Finish"
			save.setText("Finish");
			//set the delete button to be visible
			delete.setVisibility(View.VISIBLE);
		}
		//if we are creating a new report
		else{
			//set the custom header title to "Report Guide"
			header.setText("Report Guide");
			//change the save button text to be "Complete Report"
			save.setText("Complete Report");
			//remove the delete button
			delete.setVisibility(View.GONE);
		}
        
		//update the images on our buttons
		updateImages();
	}
	
	
	/**
	 * Find Activity resume handler
	 * 
	 * @param   void
	 * @return	void
	 */
	@Override
	public void onResume()
	{
		//when we resume this activity after updating information in other activities
		//update the images on our buttons
		updateImages();
		super.onResume();
	}
	
	/**
	 * onClick handler for the Location button.
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void locationReport(View view) {
		//start a new Location Activity giving it our row id
		Intent intent = new Intent(this, SCLocationActivity.class);
		intent.putExtra(SCLocationActivity.ROW_ID, rowID);
	    startActivity(intent);
	}
	
	/**
	 * onClick handler for the Culvert button.
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void culvertReport(View view) {
		//start a new Culvert Activity giving it our row id
		Intent intent = new Intent(this, SCCulvertActivity.class);
		intent.putExtra(SCLocationActivity.ROW_ID, rowID);
	    startActivity(intent);
	}
	
	/**
	 * onClick handler for the Stream button.
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void streamReport(View view) {
		//start a new Stream Activity giving it our row id
		Intent intent = new Intent(this, SCStreamActivity.class);
		intent.putExtra(SCLocationActivity.ROW_ID, rowID);
	    startActivity(intent);
	}
	
	/**
	 * onClick handler for the Road button.
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void roadReport(View view) {
		//start a new Road Activity giving it our row id
		Intent intent = new Intent(this, SCRoadDataActivity.class);
		intent.putExtra(SCLocationActivity.ROW_ID, rowID);
	    startActivity(intent);
	}
	
	/**
	 * onClick handler for the Erosion button.
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void erosionReport(View view) {
		//start a new Erosion and Conditions Activity giving it our row id
		Intent intent = new Intent(this, SCErosionandConditionsActivity.class);
		intent.putExtra(SCLocationActivity.ROW_ID, rowID);
	    startActivity(intent);
	}
	
	/**
	 * onClick handler for the Photo/Notes button.
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void photoNotesReport(View view) {
		//start a new Photo/Notes activity giving it our row id
		Intent intent = new Intent(this, SCPhotoNotesActivity.class);
		intent.putExtra(SCLocationActivity.ROW_ID, rowID);
	    startActivity(intent);
	}
	
	/**
	 * onClick handler for the Treatment button
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void treatmentReport(View view) {
		//start a new Treatment Activity giving it our row id
		Intent intent = new Intent(this, SCTreatmentsActivity.class);
		intent.putExtra(SCLocationActivity.ROW_ID, rowID);
	    startActivity(intent);
	}
	
	/**
	 * Override the back button on the phone for this activity
	 * 
	 * @param void
	 * @return	void
	 */
	@Override
    public void onBackPressed() {
		//don't allow the user to accidentally hit back and lose all their progress
    }
	
	/**
	 * Update the images next to each button pertaining to different
	 * sections of our report. The image will change depending on if
	 * the section has been filled out or not. A red X if it hasn't been
	 * filled out, a green check mark if it has been completed, a green check
	 * mark with a yellow asterisk if it is missing some key fields.
	 * 
	 * **NOTE** Still haven't implemented the algorithm or checking if certain
	 * sections of the report are missing key fields and require the green check
	 * mark and yellow asterisk image.
	 * 
	 * @param void
	 * @return	void
	 */
	public void updateImages(){
		
		//if the row id for our report exists in the table that corresponds with our button
		if(myDbHelper.rowExists(rowID, "Location")){
			//then set the image resource to be a green check mark
			locationV.setImageResource(R.drawable.check_mark);
		}
		else{
			//otherwise it will display a red x
			locationV.setImageResource(R.drawable.x_mark);
		}
		if(myDbHelper.rowExists(rowID, "Culvert_Desc")){
			culvertV.setImageResource(R.drawable.check_mark);
		}
		else{
			culvertV.setImageResource(R.drawable.x_mark);
		}
		if(myDbHelper.rowExists(rowID, "Stream_Desc")){
			streamV.setImageResource(R.drawable.check_mark);
		}
		else{
			streamV.setImageResource(R.drawable.x_mark);
		}
		if(myDbHelper.rowExists(rowID, "Road_Data")){
			roadV.setImageResource(R.drawable.check_mark);
		}
		else{
			roadV.setImageResource(R.drawable.x_mark);
		}
		if(myDbHelper.rowExists(rowID, "Treatment")){
			treatmentV.setImageResource(R.drawable.check_mark);
		}
		else{
			treatmentV.setImageResource(R.drawable.x_mark);
		}
		if(myDbHelper.rowExists(rowID, "Erosion_Desc")){
			erosionV.setImageResource(R.drawable.check_mark);
		}
		else{
			erosionV.setImageResource(R.drawable.x_mark);
		}
		if(myDbHelper.rowExists(rowID, "Photo_Notes")){
			photoNotesV.setImageResource(R.drawable.check_mark);
		}
		else{
			photoNotesV.setImageResource(R.drawable.x_mark);
		}
	}
	
	/**
	 *  onClick handler for the Complete Report button
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void completeReport(View view){
		//if we are finishing the creation of a site
		if(!editing){
			//update our creation date and time for our row
			myDbHelper.updateDateTime(rowID);
		}
		
		//make sure to close our database
		myDbHelper.close();

		//start a new Manage Reports Activity to show it has been created
		this.startActivity(new Intent(this, ManageReportsActivity.class));
		
		//end our activity
		this.finish();
	}
	
	/**
	 *  onClick handler for the Cancel button
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void cancelReport(View view){
		//if we are creating a report
		if(!editing){
			//set up a dialog box and display it to the user
			new AlertDialog.Builder(this)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle("Cancel Site")
			//ask the user if they are sure they want to delete the site
			.setMessage("Are you sure you want to cancel site creation? All data will be lost.")
			.setPositiveButton("Continue", new DialogInterface.OnClickListener()
			{
				//if they press continue
				public void onClick(DialogInterface dialog, int which) {
					//cancel our report and delete all information previously stored
					cancelReport();
					//close our database
					myDbHelper.close(); 
					
					//and finish our activity
					finish();
				}
			})
			//otherwise if they pick cancel do nothing
			.setNegativeButton("Cancel", null)
			.show();
		}
		//if we are editing a report
		else{
			//close our database
			myDbHelper.close();
			//and start a new Manage Reports Activity
			this.startActivity(new Intent(this, ManageReportsActivity.class));
			
			this.finish();
		}
	}
	
	/**
	 *  onClick handler for the Delete button
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void deleteReport(View view)
	{
		//set up a dialog box to display to the user
		new AlertDialog.Builder(this)
		.setIcon(android.R.drawable.ic_dialog_alert)
		.setTitle("Delete Report")
		//ask the user if they are sure they want to delete this report
		.setMessage("Are you sure you want to delete this report permanently?")
		.setPositiveButton("Continue", new DialogInterface.OnClickListener()
		{
			//if they press continue
			public void onClick(DialogInterface dialog, int which) {
				//delete the report
				deleteData();
				//close our database
				myDbHelper.close();
				
				finish();    
			}
		})
		//otherwise do nothing
		.setNegativeButton("Cancel", null)
		.show();
	}
	
	/**
	 *  Delete all data regarding our report.
	 * 
	 * @param void
	 * @return	void
	 */
	public void deleteData(){
		//delete the report with our row id
		myDbHelper.deleteReport(rowID);
		
		//start a new Mange Reports Activity
		Intent intent = new Intent(this, ManageReportsActivity.class);
		//set intent flags to finish all activities that existed before
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
		this.startActivity(intent);
	}
	
	/**
	 * Cancel our report in report creation.
	 * 
	 * @param void
	 * @return	void
	 */
	public void cancelReport(){
		//if we are canceling our report in report creation delete all the stored data
		myDbHelper.deleteReport(rowID);
	}
	
	/**
	 *  onClick handler for the Tutorial button
	 * 
	 * @param view - View - responsible for drawing UI components and event handling
	 * @return	void
	 */
	public void tutorial(View view) {
		//start a new Tutorial Activity
		this.startActivity(new Intent(this, SCTutorialActivity.class));
	}
	
}
